<%@ page language="java" isELIgnored="false" import="java.util.*" pageEncoding="UTF-8" contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>用户信息管理</title>
     <%@include file="/common/include.jsp"%>
     <!-- 圆角文本框 -->
<link href="<%=request.getContextPath() %>/fare/css/htc/htc.css" rel="stylesheet" type="text/css" />
     	<script>
		$(function(){
		    addDeptSel();
			$('#userForm').hide();
			$('#userList').datagrid({
				//title: '用户列表',
				pagination:true,
				nowrap: false,
                striped: true,
                collapsible: true,
				toolbar:[{
					text:'新增',
					iconCls:'icon-add',
					handler:function(){
						WinOpen();
					}
				},'-',{
					text:'修改',
					iconCls:'icon-edit',
					handler:function(){
						modify();
					}
				},'-',{
					text:'删除',
					iconCls:'icon-remove',
					handler:function(){
						remove();
					}
				},'-',{
					text:'查询',
					iconCls:'icon-search',
					handler:function(){
					    $('#userList').datagrid('options').url = 'admin/userAction!getUserListJSON.action?isSearch=true&_query.departmentNames='+$('#departmentids').combobox('getText')+'&_query.names='+$('#names').val();
					    $('#userList').datagrid("reload");

					}
				}],
				 rownumbers:false,
				 frozenColumns:[[
				    {field:'ck',checkbox:true},
				    {title:'姓名',field:'names',width:"80"}
				]],
				onLoadSuccess: function () {
					$('#saveGroup').linkbutton({disabled:true});
					reloadTree();
					//$('body').layout('collapse','east');
				    $('.datagrid-toolbar').append($('#searchTable'));
				    $('#searchTable').show();
				}
			});
			$('#groupTree').tree({
				checkbox: true,
				url: 'admin/groupAction!groupTreeJSON.action',
				onLoadSuccess:function(){
					$(this).tree('expandAll');
				}
			});		
		});
		
		function addDeptSel(){
			var url ='admin/userAction!findDepartmentJSON';
			$("#dept_id").empty();
			$.post(url,{},function(json){
				for(var index in json){
					if(json[index].ids!="" && json[index].ids!="undefined" && json[index].ids!=undefined){
						$("#dept_id").append('<option value='+json[index].ids+'>'+json[index].names+'</option>' );
					}
					
				}
			});
		}
		
		function reloadTree(){
			var userIds = $('#groupForm').attr('user.ids').value;
			if(userIds){
	        	$('#groupForm').form('clear');
				$('#groupTree').tree('options').url="admin/groupAction!groupTreeJSON.action";
				$('#groupTree').tree('reload');
				//$('body').layout('collapse','east');
			}
		}
		function SpecCHK(obj){
			if(obj.checked){
				$('#SpecField').css({'border':'1px solid #B5B8C8;'});
				$('#SpecInfo').show();
			}else{
				$('#SpecField').css({'border':'0px solid #B5B8C8;'});
				$('#SpecInfo').hide();
			}		
		}
		function formatterValid(rowIndex,rowData){
			return '<a href="#" onclick="transValid('+rowData.ids+',\''+rowData.valid+'\')">'+rowData.valids+'</a>';
		}
		function formatterAct(rowIndex,rowData){
			return '<a href="#" onclick="asignGroup('+rowData.ids+',\''+rowData.names+'\')"><img alt="分配组..." src="${pageContext.request.contextPath }/images/kcmsystem.png" border="0"></img></a>';
		}
		function asignGroup(ids,names){
			$('#userList').datagrid('unselectAll');
			if(names.length>6)
				names = names.substr(0,5)+'...';
			$('#groupTreePanel').panel('setTitle','分配【'+names+'】 用户组');
			var userIds = $('#groupForm').attr('user.ids').value;
			if(!userIds)
				$('body').layout('expand','east');
			$('#groupForm').attr('user.ids').value = ids;//用户ID
			$('#groupTree').tree('options').url="admin/groupAction!groupTreeJSON.action?user.ids="+ids;
			$('#groupTree').tree('reload');
			$('#saveGroup').linkbutton({disabled:false});
		}
		function transValid(ids,valid){
			if(ids&&valid){
				var msg = '';
				if(valid=='0'){
					msg = '确定置用户为有效？';
					valid='1';
				}else{
					valid='0';
					msg = '确定置用户为无效？';
				}
				$.messager.confirm('提示信息', '<div style="padding-top:15px;padding-left:5px"><b>'+msg+'</b></div>', function(r){
					if (r){
						var url="admin/userAction!updateValid.action";
						$.post(
								url,
								{"user.ids":ids,"user.valid":valid},
								function(data){
									eval('data='+data);
						        	var state = data.state;
						        	var retMsg = data.resultMsg;
						        	if(state==true){
						        		$('#userList').datagrid("reload");
						        	}
						        	$.messager.alert('提示信息','<div style="padding-top:15px;padding-left:5px"><b>'+retMsg+'</b></div>','info');
								}
						);
	
					}
				});
			}
		}
		function WinOpen(ids){
			   $('#userForm').form('clear');
			   $('#userWindows').window({draggable:false});
			   $('#userWindows').window('open');
			   $('#userWindows').panel('setTitle','增加用户');
			   $('#newPwdLable').hide();
			   $('#newPwdLable2').hide();
			   $('#pwdLable').show();
			   $('#pwdLable2').show();
			   if(ids){
				   $('#newPwdLable').show();
				   $('#newPwdLable2').show();
				   $('#pwdLable').hide();
				   $('#pwdLable2').hide();
				   $('#userWindows').panel('setTitle','修改用户');
			   	   $('#userForm').form('load', 'admin/userAction!viewUser.action?user.ids='+ids); 
			   }
		       $('#userForm').show();
		}
		function modify(){
			var selected = $('#userList').datagrid('getSelected');
			var rows = $('#userList').datagrid('getSelections');
			if (rows.length<2&&selected){
				WinOpen(selected.ids);
		    }else{
		    	$.messager.alert('提示信息','<div style="padding-top:15px;padding-left:5px"><b>请选择一条数据！</b></div>','warning');
		    }
		}
		function remove(){
			var selectedRows = $('#userList').datagrid('getSelections');
			var delIds = '';
			if (selectedRows.length>0){
				for(var i=0; i<selectedRows.length; i++){
					if (delIds != '') delIds += ',';
					delIds += selectedRows[i].ids;
				}
				$.messager.confirm('提示信息', '<div style="padding-top:15px;padding-left:5px"><b>确定删除？</b></div>', function(r){
					if (r){
						var url="admin/userAction!deleteUser.action";
						$.post(
								url,
								{"delIds":delIds},
								function(data){
									eval('data='+data);
						        	var state = data.state;
						        	var retMsg = data.resultMsg;
						        	if(state==true){
						        		$('#userList').datagrid("reload");
						        		$('#groupForm').form('clear');
						        		reloadTree();
						        	}
						        	$.messager.alert('提示信息','<div style="padding-top:15px;padding-left:5px"><b>'+retMsg+'</b></div>','info');
								}
						);
					}
				});

			}else{
		    	$.messager.alert('提示信息','<div style="padding-top:15px;padding-left:5px"><b>请至少选择一条数据！</b></div>','warning');
		    }
		}
		function saveGroup(){
			var nodes = $('#groupTree').tree('getChecked');
            var ids = '';
            var msg = "将删除该用户所拥有的组,确定操作？";
			if(nodes){
				for(var i=0; i<nodes.length; i++){
					if (ids != '') ids += ',';
					ids += nodes[i].id;
				}
			}
		    if(ids)
		        msg="确定分配该组的角色？";
			$.messager.confirm('提示信息', '<div style="padding-top:15px;padding-left:5px"><b>'+msg+'</b></div>', function(r){
				if (r){
					$('#groupForm').attr('groupIds').value = ids;
					$('#groupForm').form('submit',{
				        success:function(data){
				        	eval('data='+data);
				        	var state = data.state;
				        	var retMsg = data.resultMsg;
				        	$.messager.alert('提示信息','<div style="padding-top:15px;padding-left:5px"><b>'+retMsg+'</b></div>','info');
				        	if(state==true){
				        		var userIds = $('#groupForm').attr('user.ids').value;//用户ID
				    			$('#groupTree').tree('options').url="admin/groupAction!groupTreeJSON.action?user.ids="+userIds;
				        		$('#groupTree').tree('reload');
					        	//$('#groupForm').form('clear');
				        	}
				        }
					});
				}
			});
		}
		function saveUser(){
			
			//var pwd=$("#newPwdLable").val();
			///var re_pwd=$("#newPwdLable2").val();
			var pwd=$("#passWord").val();
			var re_pwd=$("#passWord2").val();
			if(pwd==""||re_pwd==""){
				$.messager.alert('错误信息','<div style="padding-top:15px;padding-left:5px"><b>密码不能为空!</b></div>','error');
				return false;
			}
			
			//添加和修改保存
			$('#userForm').form('submit',{
		        onSubmit: function(){     	
		        	return $('#userForm').form('validate');
		        },
		        success:function(data){
		        	eval('data='+data);
		        	var state = data.state;
		        	var retMsg = data.resultMsg;
		        	$.messager.alert('提示信息','<div style="padding-top:15px;padding-left:5px"><b>'+retMsg+'</b></div>','info');
		        	if(state==true){
		    			$('#userList').datagrid('reload');
						$('#userWindows').window('close');
						$('#groupForm').form('clear');
			        	$('#userForm').form('clear');
		        	}
		        }
			});
		}
	</script>
</head>
<body class="easyui-layout">
  <div id="userWindows" class="easyui-window" closed="true" iconCls="icon-save" draggable="false" modal="true" resizable="false" maximizable
  ="false" minimizable
  ="false" title="添加用户" style="width:600px;height:350px;padding:2px;background:#fafafa;">
	 <div region="center" border="false" style="padding:2px; background:#fff; border:1px solid #ccc;">
	 	<%@include file="userInfo.jsp"%>
     </div>
     <div region="south" border="false" style="text-align:right;height:30px;line-height:30px;">
     	<a class="easyui-linkbutton" icon="icon-save" href="javascript:void(0)" onclick="saveUser()">保存</a>
     	<a class="easyui-linkbutton" icon="icon-back" href="javascript:void(0);" onclick="javascript:$('#userWindows').window('close');">返回</a>
     </div>
  </div>
  <div region="center" title="用户列表" style="padding:5px;" border="false">
	<table id="userList" fit="true" url="admin/userAction!getUserListJSON.action">
		<thead>
			<tr>
				<th field="userName" width="120">登录名</th>
				<th field="departmentNames" width="120">部门</th>
				<th field="userClass" width="80">用户类型</th>
				<th field="postion" width="100">职务</th>
				<th field="email" width="80">邮箱</th>
				<th field="telephone" width="100">手机</th>
				<th field="address" width="150">地址</th>
				<th field="group" width="50" formatter="formatterAct" align="center">组操作</th>
			</tr>
		</thead>
	</table>	
</div>
  <div id="groupTreePanel" region="east" title="组列表树" border=false split="true" style="width:230px;padding:5px;">
    <form id="groupForm" method="post" action="admin/userAction!userGroupSave.action">
    	<input type="hidden" name="user.ids">
    	<input type="hidden" name="groupIds">
		<ul id="groupTree"></ul>
	</form>
  </div>
<table id="searchTable" style="display:none" border="0" cellpadding="0">
	<tr>
		<td width="20%">
			<B><select id="departmentids" class="easyui-combotree" url="admin/commAction!getDepartmentTreeJSON.action" valueField="id" textField="text"  style="width:160px;"></select></B>
		</td>
		<td width="15%">
			<B>所属部门</B>
		</td>
	    <td width="20%">
			<B><input type="text" id="names"/></B>
		</td>
		<td width="15%">
			<B>用户名称</B>
		</td>
		<td width="30%" align="right">
			<a href="#" onclick="saveGroup()" class="easyui-linkbutton" id="saveGroup" plain="true" iconCls="icon-save" style="disabled">保存分配组</a>
		</td>	
	<tr>
</table>
</body>
</html>